package org.hawk.gwt.ppc.parser.impl;

import java.io.StringReader;

import junit.framework.TestCase;

import org.hawk.gwt.ppc.parser.NoSuchTokenException;
import org.hawk.gwt.ppc.utils.PreprocessorException;

public class TokenizerTestCase extends TestCase {
	public void testBase() throws PreprocessorException {
		Tokenizer tokenizer = new Tokenizer(new StringReader("a b c d"));
		
		assertEquals("a", tokenizer.next().toString());
		assertEquals("b", tokenizer.next().toString());
		assertEquals("c", tokenizer.next().toString());
		assertEquals("d", tokenizer.next().toString());
		assertEquals(false, tokenizer.hasNext());
		try {
			tokenizer.next();
			fail();
		} catch (NoSuchTokenException ex) {
		}		
	}
	
	public void testNames() throws PreprocessorException {
		Tokenizer tokenizer = new Tokenizer(new StringReader(" aaa bbb ccc ddd   "));
		
		assertEquals("aaa", tokenizer.next().toString());
		assertEquals("bbb", tokenizer.next().toString());
		assertEquals("ccc", tokenizer.next().toString());
		assertEquals("ddd", tokenizer.next().toString());
		assertEquals(false, tokenizer.hasNext());
	}
	
	public void testOperators() throws PreprocessorException {
		Tokenizer tokenizer = new Tokenizer(new StringReader(
				" ++ -- +expr -expr ~!*/%+- << >> >>> < > <= >= " +
				"instanceof == != &^|&&||?: = += -= *= /= %= &= ^= |= <<= >>= >>>=  "));
		
		assertEquals("++", tokenizer.next().toString());
		assertEquals("--", tokenizer.next().toString());
		assertEquals("+", tokenizer.next().toString());
		assertEquals("expr", tokenizer.next().toString());
		assertEquals("-", tokenizer.next().toString());
		assertEquals("expr", tokenizer.next().toString());
		assertEquals("~", tokenizer.next().toString());
		assertEquals("!", tokenizer.next().toString());
		assertEquals("*", tokenizer.next().toString());
		assertEquals("/", tokenizer.next().toString());
		assertEquals("%", tokenizer.next().toString());
		assertEquals("+", tokenizer.next().toString());
		assertEquals("-", tokenizer.next().toString());
		assertEquals("<<", tokenizer.next().toString());
		assertEquals(">>", tokenizer.next().toString());
		assertEquals(">>>", tokenizer.next().toString());
		assertEquals("<", tokenizer.next().toString());
		assertEquals(">", tokenizer.next().toString());
		assertEquals("<=", tokenizer.next().toString());
		assertEquals(">=", tokenizer.next().toString());
		assertEquals("instanceof", tokenizer.next().toString());
		assertEquals("==", tokenizer.next().toString());
		assertEquals("!=", tokenizer.next().toString());
		assertEquals("&", tokenizer.next().toString());
		assertEquals("^", tokenizer.next().toString());
		assertEquals("|", tokenizer.next().toString());
		assertEquals("&&", tokenizer.next().toString());
		assertEquals("||", tokenizer.next().toString());
		assertEquals("?", tokenizer.next().toString());
		assertEquals(":", tokenizer.next().toString());
		assertEquals("=", tokenizer.next().toString());
		assertEquals("+=", tokenizer.next().toString());
		assertEquals("-=", tokenizer.next().toString());
		assertEquals("*=", tokenizer.next().toString());
		assertEquals("/=", tokenizer.next().toString());
		assertEquals("%=", tokenizer.next().toString());
		assertEquals("&=", tokenizer.next().toString());
		assertEquals("^=", tokenizer.next().toString());
		assertEquals("|=", tokenizer.next().toString());
		assertEquals("<<=", tokenizer.next().toString());
		assertEquals(">>=", tokenizer.next().toString());
		assertEquals(">>>=", tokenizer.next().toString());
		
		assertEquals(false, tokenizer.hasNext());
	}
	
	
	public void testLiterals() throws PreprocessorException {
		Tokenizer tokenizer = new Tokenizer(new StringReader(
				" 2aaa 141234 'a' \"asdfjlkasjdf \\\\\\\"\"   "));
		
		assertEquals("2aaa", tokenizer.next().toString());
		assertEquals("141234", tokenizer.next().toString());
		assertEquals("'a'", tokenizer.next().toString());
		assertEquals("\"asdfjlkasjdf \\\\\\\"\"", tokenizer.next().toString());
		
		assertEquals(false, tokenizer.hasNext());
	}
	
	public void testComments() throws PreprocessorException {
		Tokenizer tokenizer = new Tokenizer(new StringReader(" // this is comment\n" +
				"aaa /* this is other \n multi line \n comment */   bbb"));
		
		assertEquals("// this is comment", tokenizer.next().toString());
		assertEquals("aaa", tokenizer.next().toString());
		assertEquals(2, tokenizer.current().getLine());
		assertEquals(1, tokenizer.current().getOffset());
		
		assertEquals("/* this is other \n multi line \n comment */", tokenizer.next().toString());
		
		assertEquals("bbb", tokenizer.next().toString());
		assertEquals(4, tokenizer.current().getLine());
		assertEquals(15, tokenizer.current().getOffset());
		
		assertEquals(false, tokenizer.hasNext());
	}
	
	public void testLineAndOffset() throws PreprocessorException {
		Tokenizer tokenizer = new Tokenizer(new StringReader(" // this is comment\n" +
				"aaa /* this is other \n multi line \n comment */   bbb\n" +
				"ccc 23\n   \n   \n   \r\n \r aa"));
		
		assertEquals("// this is comment", tokenizer.next().toString());
		assertEquals(1, tokenizer.current().getLine());
		assertEquals(2, tokenizer.current().getOffset());
		
		assertEquals("aaa", tokenizer.next().toString());
		assertEquals(2, tokenizer.current().getLine());
		assertEquals(1, tokenizer.current().getOffset());
		
		assertEquals("/* this is other \n multi line \n comment */", 
				tokenizer.next().toString());
		assertEquals(2, tokenizer.current().getLine());
		assertEquals(5, tokenizer.current().getOffset());

		assertEquals("bbb", tokenizer.next().toString());
		assertEquals(4, tokenizer.current().getLine());
		assertEquals(15, tokenizer.current().getOffset());

		assertEquals("ccc", tokenizer.next().toString());
		assertEquals(5, tokenizer.current().getLine());
		assertEquals(1, tokenizer.current().getOffset());

		assertEquals("23", tokenizer.next().toString());
		assertEquals(5, tokenizer.current().getLine());
		assertEquals(5, tokenizer.current().getOffset());
		
		assertEquals("aa", tokenizer.next().toString());
		assertEquals(10, tokenizer.current().getLine());
		assertEquals(2, tokenizer.current().getOffset());
	}
}
